//
//  VideoRecordController.m
//  cooba-iOS
//
//  Created by 郭四虎 on 16/4/1.
//  Copyright © 2016年 YouKu. All rights reserved.
//

#import "VideoRecordController.h"
#import "MyVideoRecord.h"
#import "VideoListModel.h"
#import "VideoRecordCell.h"
#import "VideoDetailController.h"

@interface VideoRecordController ()<UITableViewDataSource,UITableViewDelegate>
{
    UILabel * _msgLb;
}
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) NSArray *dataArr;

@end

@implementation VideoRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.dataArr = [MyVideoRecord shareRecord].dataArr;
    [_table registerNib:[UINib nibWithNibName:@"VideoRecordCell" bundle:nil] forCellReuseIdentifier:@"VideoRecordCell"];
    [_table delsetExtraline];
    self.title = @"播放记录";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"清除" style:UIBarButtonItemStyleDone target:self action:@selector(cleanRecord)];
    self.navigationItem.rightBarButtonItem = right;
    
    [self checkData];
}

- (void)checkData
{
    if (!_dataArr.count) {
        
        _msgLb = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, WIDTH - 40, 50)];
        _msgLb.text = @"暂时没有记录";
        _msgLb.textAlignment = NSTextAlignmentCenter;
        _msgLb.textColor = [UIColor lightGrayColor];
        _msgLb.font = [UIFont systemFontOfSize:18];
        [_table addSubview:_msgLb];
    }else{
        [_msgLb removeFromSuperview];
    }
}

- (void)cleanRecord
{
    if (!_dataArr.count) {
        return;
    }
    [Myuser alertWithTitle:@"清空列表" Msg:@"确认操作?" target:self action:^{
        
        [[MyVideoRecord shareRecord]cleanVideoRecord];
        [_table reloadData];
        [self checkData];
    }];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoRecordCell" forIndexPath:indexPath];
    
    VideoListModel *model = _dataArr[indexPath.row];
    [cell showModel:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*SCALEY;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoListModel *model = _dataArr[indexPath.row];
    
    VideoDetailController *controller = [[VideoDetailController alloc]init];
    controller.model = model;
    
    [self.navigationController pushViewController:controller animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoListModel *model = _dataArr[indexPath.row];
    [[MyVideoRecord shareRecord]delOneRecord:model];
    [tableView reloadData];
    [self checkData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[MyVideoRecord shareRecord]saveVideoRecord];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
